package jetpack.com.pzl.works;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import java.util.Arrays;

/**
 * 一个模拟上传任务的工作类
 */
public class UploadWorker extends Worker {
    final String TAG = "jinx_UploadWorker";


    public UploadWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.i(TAG, "UploadWorker doWork : " + Thread.currentThread().getName());

        String param1 = getInputData().getString("result1");
        String param2 = getInputData().getString("result2");

        String uploadParam = param1 + " - " + param2;
        uploadFiles(uploadParam);
        Log.i(TAG, "UploadWorker doWork : " + Thread.currentThread().getName() + " 上传完成");
        //一个数据结果的封装类，可自行填充数据，此处给个空 标记为success
        return Result.success();
    }


    /**
     * 模拟上传文件
     */
    private void uploadFiles(String content) {
        int count = 0;
        while (count <= 10) {
            Log.i(TAG, "上传文件  " + count * 10 + "%..... " + (TextUtils.isEmpty(content) ? "" : content));
            try {
                Thread.sleep(330);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            count++;
        }
    }

}
